/******************************************************************************
* This file is part of is32p218 platform,
* Copyright (c) 2009 HangZhou InfoStrong CO.,LTD. All rights reserved.
* This software may only be used under the terms of a valid, current,
* end user license from InfoStrong.
* Nothing else gives you the right to use this software.   
*
* Name:   plc_main.c
* Desc:   PLC Main C file
* Author: Jerry
* Date:   2009-10-27
* Note:   
* History:
*
******************************************************************************/
#include <string.h>
#include "os_api.h"
#include "sys_glb.h"
#include "plc.h"
#include "plc_if.h"
#include "plc_dl.h"
#include "plc_hl.h"
#include "drv_all.h"

static void plc_main_tsk( void *param );

static isOS_TCB g_plc_tsk;
static isOS_STACK g_plc_hl_tsk_stack[TSK_STK_SIZE_PLC_HL];

const isOS_TskInfo c_plc_hl_tskinfo=
{
	plc_main_tsk,					//-- task function
	NULL,							//-- task function parameter
	g_plc_hl_tsk_stack,				//-- task stack end addr in memory
	TSK_STK_SIZE_PLC_HL,			//-- task stack size (in isOS_S32,not bytes)
	"PLC Main Task",				//-- task name
	TSK_PRIO_PLC_HL,				//-- task priority
};

static MSG_HDR_T g_plc_que_buff[PLC_MSG_ITEM_LEN];
static isOS_QUE g_plc_que;
BOOL g_plc_hl_task_alive_flg = FALSE;

/*
* Function Name:
*   plc_module_init
* Description:
*   PLC Module initial
* Parameters:
* Returns:
* Author				Date 
*  Jerry				2009-10-26   
* Note:
*    
*/
void plc_module_init( void )
{
	isOS_S32 ret;
	ret = isOS_QueueCreate( &g_plc_que, (isOS_U8*)g_plc_que_buff, \
							 PLC_MSG_ITEM_LEN, sizeof(MSG_HDR_T) );
	if ( ret != pdPASS )
	{
		IS_ASSERT(0);
	}

	ret = isOS_TaskCreate( &g_plc_tsk, \
						   &c_plc_hl_tskinfo,
						   OS_TASK_START_ON_CREATION );
	if ( ret != pdPASS )
	{
		IS_ASSERT(0);
	}	
		
	plc_hl_init();
	plc_dl_init();
	plc_if_init();


}
void plc_msg_snd_alive_check(void)
{
	plc_msg_snd(MSG_IDLE_TO_PLC_HL,NULL,0);				// to plc_hl_tsk				
}
/*
* Function Name:
*   plc_main_tsk
* Description:
*   PLC main task
* Parameters:
*   void *param:
* Returns:
* Author				Date 
*  Jerry				2009-10-26   
* Note:
*    
*/
static void plc_main_tsk( void *param )
{
	isOS_S32 result;
	MSG_HDR_T msg_hdr;
	U8 *p_msg_data;

	for(;;)
	{
		result = isOS_QueueReceive( &g_plc_que, &msg_hdr,  \
							   TM_WAIT_FOREVER );
		drv_sys_wdt_feed();
		if (pdPASS == result )
		{	// return Seccess
			if ( msg_hdr.m_len )
			{	// Data region is validate, data from pointer of m_pointer
				p_msg_data = (U8*)msg_hdr.m_pointer;
			}
			else
			{	// No have data region, dword parameter will convey through m_pointer(U32)
				p_msg_data = (U8*)&msg_hdr.m_pointer;
			}
			
			switch( msg_hdr.m_msg_id&0xFF00)
			{
				case MSG_AFN_TO_PLC:
				{
					// PLC HL Message
					result = plc_hl_app_proc( p_msg_data, \
										  msg_hdr.m_len, (MSG_PLC_T)msg_hdr.m_msg_id );
					break;
				}
				case MSG_PLCIF_TO_HL:
				{
					result = plc_hl_dl_proc( p_msg_data, msg_hdr.m_len, (MSG_PLC_T)msg_hdr.m_msg_id );
					break;
				}
				case MSG_PLC_TO_PLC:
				{
					result = plc_hl_hl_proc( p_msg_data, \
									  msg_hdr.m_len, (MSG_PLC_T)msg_hdr.m_msg_id );
					break;
				}
				case MSG_IDLE_TO_PLC_HL:
				{
					g_plc_hl_task_alive_flg = TRUE;
					break;
				}
				default:
				{
					IS_ASSERT(0);
					break;
				}
				
			}
		}
	} 
}

/*
* Function Name:
*   plc_msg_snd
* Description:
*   PLC HL Message send
* Parameters:
*   MSG_HDR_T* p_msg_hdr:
*   U8* p_msg_data:
* Returns:
*   BOOL:
* Author				Date 
*  Jerry				2009-10-27   
* Note:
*    
*/
BOOL plc_msg_snd( U16 msg_id, U8 *p_msg_data, U16 msg_len )
{
	MSG_HDR_T msg_hdr;
	msg_hdr.m_msg_id = msg_id;
	if ( msg_len )
	{
		msg_hdr.m_len = msg_len;
		msg_hdr.m_pointer = (U32)p_msg_data;
	}
	else
	{
		msg_hdr.m_len = 0;
		msg_hdr.m_pointer = (U32)*p_msg_data++;
		msg_hdr.m_pointer += ((U32)*p_msg_data++<<8);
		msg_hdr.m_pointer += ((U32)*p_msg_data++<<16);
		msg_hdr.m_pointer += ((U32)*p_msg_data++<<24);
	}
	if ( pdPASS != isOS_QueueSend( &g_plc_que, &msg_hdr, TM_POLLING ) )
	{
		IS_ASSERT(0);
		return FALSE;
	}
	return TRUE;
}

/*
* Function Name:
*   plc_main_que_empty
* Description:
*   plc main queue empty
* Parameters:
* Returns:
*   BOOL: TRUE( empty ), FALSE( not empty )
* Author				Date 
*  Jerry				2011-5-31   
* Note:
*    
*/
BOOL plc_main_que_empty( void )
{
	return isOS_QueueIsEmpty(&g_plc_que);
}
